package com.wpf.servlet;

import com.wpf.bean.Userbean;
import com.wpf.dao.UserDAO;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

public class AutoLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public AutoLoginServlet() {}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
		HttpSession session = request.getSession();
		UserDAO userdao = new UserDAO();
		Userbean userbean = new Userbean();
		Pattern pattern = Pattern.compile("^[1-9]\\d*|0$");
		String ID = request.getParameter("id");
		String passWord = request.getParameter("password");
		if(ID==""||ID==null) {
			request.setAttribute("logintip", "<font size='1' color='red'>学号不能为空！</font>");
			request.getRequestDispatcher("/wpf_login.jsp").forward(request, response);
		}else if(!pattern.matcher(ID).matches()) {
			request.setAttribute("logintip", "<font size='1' color='red'>学号格式不正确！</font>");
			request.getRequestDispatcher("/wpf_login.jsp").forward(request, response);
		}else {
			int IDNum = Integer.parseInt(ID);
			userbean = userdao.find(IDNum);
			if(userbean==null) {
				request.setAttribute("logintip", "<font size='1' color='red'><strong>学号</strong>不存在或密码错误！</font>");
				request.getRequestDispatcher("/wpf_login.jsp").forward(request, response);
			}else if(userbean!=null&&!userbean.getPassword().equals(passWord)){
				request.setAttribute("logintip", "<font size='1' color='red'>学号不存在或<strong>密码</strong>错误！</font>");
				request.getRequestDispatcher("/wpf_login.jsp").forward(request, response);
			}else if(userbean!=null&&userbean.getPassword().equals(passWord)){//账户与密码皆正确
				session.setAttribute("username", userbean.getUsername());
				String keepTime = request.getParameter("keeplogin");
				//如果用户记住口令，对密码加密并写入Cookie
				if(keepTime!=null){
					int turnkeepTime = 0;
					switch (keepTime) {
						case "60*60*24": {
							turnkeepTime = 60*60*24;
							break;
						}
						case "60*60*24*3": {
							turnkeepTime = 60*60*24*3;
							break;
						}
						case "60*60*24*7": {
							turnkeepTime = 60*60*24*7;
							break;
						}
					}
					String loginToken = UserDAO.encryptKaisa(ID,1)+"%"+UserDAO.encryptKaisa(passWord,1);
					Cookie cookieUP = new Cookie("logintoken",URLEncoder.encode(loginToken,"utf-8"));
					cookieUP.setPath(request.getContextPath());
					cookieUP.setMaxAge(turnkeepTime);
					response.addCookie(cookieUP);
				}
				//记住上次登录时间代码
				String lastLoginTime = null;
				Cookie[] cookies = request.getCookies();
				
				for(int i=0;cookies!=null&&i<cookies.length;i++) {
					if("lastlogintime".equals(cookies[i].getName())) {
						lastLoginTime=cookies[i].getValue();
						break;
					}
				}
				String askTip = "";
				if(lastLoginTime==null) {
					askTip="<p><font size='2' color='green'><strong>这是你首次访问本系统</strong></font></p>";
				}else {
					askTip="<p>上次登录时间：<font size='2' color='green'><strong>"+lastLoginTime+"</strong></font></p>";
				}
				session.setAttribute("asktip", askTip);
				String nowTime = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format(new Date());
				Cookie cookieTime = new Cookie("lastlogintime", nowTime);
				cookieTime.setPath(request.getContextPath());
				cookieTime.setMaxAge(60*60*24*30);
				response.addCookie(cookieTime);
				response.sendRedirect("wpf_index.jsp");
			}
		}
	}
}
